package com.zdb.order.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdb.order.entity.Address;
import com.zdb.order.entity.Citys;
import com.zdb.order.entity.District;
import com.zdb.order.entity.Provinces;
import com.zdb.order.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


@Service

public class AddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private ProvincesService provincesService;
    @Autowired
    private CitysService citysService;
    @Autowired
    private DistrictService districtService;

    //根据uid查询所有收货地址
    public List<Address> selectAddressLisrts(int uid){

        List<Address> addresses = addressMapper.selectList(new QueryWrapper<Address>().like("uid", uid));

        List<Address> resultAddress = addresses.stream().map(item -> {
            Provinces provinces = provincesService.queryProvinces(item.getPid());
            if (!StringUtils.isEmpty(provinces)) {
                item.setpAddress(provinces.getName());
            }

            Citys citys = citysService.queryCitys(item.getCid());
            if (!StringUtils.isEmpty(citys)) {
                item.setcAddress(citys.getName());
            }
            District district = districtService.queryDistrict(item.getDid());
            if (!StringUtils.isEmpty(district)) {
                item.setdAddress(district.getName());
            }
            return item;
        }).collect(Collectors.toList());

        return addresses;
    }
    //根据uid删除关联的所有收货地址
    public int deleteAddressByUserId(int uid){
        int resultCount = addressMapper.selectCount(new QueryWrapper<Address>().like("uid", uid));
        return resultCount;
    }

}
